
enum OsnCardType: String {
    case User = "user"
    case Group = "group"
    case ChatRoom = "chatroom"
    case Channel = "channel"
    case Litapp = "litapp"
    case Share = "share"
    
    init(rawValue: String) {
        switch rawValue {
        case "user": self = .User
        case "group": self = .Group
        case "chatroom": self = .ChatRoom
        case "channel": self = .Channel
        case "litapp": self = .Litapp
        case "share": self = .Share
        default: self = .User
        }
    }
}
class OsnCardMessageContent: OsnMessageContent {
    var type = "card"
    var target = ""
    var name = ""
    var param = ""
    var displayName = ""
    var portrait = ""
    var theme = ""
    var url = ""
    var info = ""
    var fromUser = ""
    var cardType = ""//OsnCardType.User

    override init(){
        super.init()
        contentType = OsnMessageContentType.card
    }
    override func toJson() ->[String:Any] {
        var json = super.toJson()
        json["type"] = type
        json["target"] = target
        json["name"] = name
        json["param"] = param
        json["displayName"] = displayName
        json["portrait"] = portrait
        json["theme"] = theme
        json["url"] = url
        json["info"] = info
        json["fromUser"] = fromUser
        return json
    }
    static func fromJson(_ data:[String:Any]) ->OsnCardMessageContent {
        let c = OsnCardMessageContent()
        super.fromJson(c, data)
        //let type = data["cardType"] as? String ?? data["type"] as? String ?? ""
        c.target = data["target"] as? String ?? ""
//        if(type == "card"){
            if(c.target.hasPrefix("OSNU")){
                c.cardType = "user"//OsnCardType.User
            }else if(c.target.hasPrefix("OSNG")){
                c.cardType = "group"//OsnCardType.Group
            }else{
                c.cardType = "litapp"//OsnCardType.Litapp
            }
//        }else{
//            c.type = OsnCardType(rawValue: type)
//        }
        c.name = data["name"] as? String ?? ""
        c.param = data["param"] as? String ?? ""
        c.displayName = data["displayName"] as? String ?? ""
        c.portrait = data["portrait"] as? String ?? ""
        c.theme = data["theme"] as? String ?? ""
        c.url = data["url"] as? String ?? ""
        c.info = data["info"] as? String ?? ""
        return c
    }
}
